package com.personal.portal.admin.controller;

import com.personal.portal.common.entity.Result;
import com.personal.portal.admin.entity.ContactMessage;
import com.personal.portal.admin.service.ContactMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 联系消息控制器
 * 
 * @author zhanghui
 * @version 1.0.0
 * @since 2025-11-10
 */
@Tag(name = "联系消息", description = "联系消息信息管理接口")
@RestController
@RequestMapping("/admin/contact-message")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class ContactMessageController {

    @Resource
    private ContactMessageService contactMessageService;

    /**
     * 根据ID查询联系消息
     */
    @Operation(summary = "根据ID查询联系消息", description = "根据联系消息ID查询详细信息")
    @GetMapping("/{id}")
    public Result<ContactMessage> queryById(
            @Parameter(description = "联系消息ID", required = true) 
            @PathVariable Long id) {
        try {
            ContactMessage contactMessage = contactMessageService.queryById(id);
            if (contactMessage != null) {
                return Result.success(contactMessage);
            } else {
                return Result.error("联系消息信息不存在");
            }
        } catch (Exception e) {
            return Result.error("查询联系消息信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据条件查询联系消息列表
     */
    @Operation(summary = "根据条件查询联系消息列表", description = "根据发送者姓名、邮箱等条件查询联系消息信息")
    @PostMapping("/list")
    public Result<List<ContactMessage>> queryList(
            @Parameter(description = "联系消息查询条件") 
            @RequestBody ContactMessage contactMessage) {
        try {
            List<ContactMessage> contactMessageList = contactMessageService.queryList(contactMessage);
            return Result.success(contactMessageList);
        } catch (Exception e) {
            return Result.error("查询联系消息列表失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询联系消息
     */
    @Operation(summary = "分页查询联系消息", description = "支持分页和条件查询的联系消息信息")
    @PostMapping("/page")
    public Result<Result.PageResult<ContactMessage>> queryPage(
            @Parameter(description = "页码，从1开始", example = "1") 
            @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "10") 
            @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "联系消息查询条件") 
            @RequestBody ContactMessage contactMessage) {
        try {
            Result.PageResult<ContactMessage> pageResult = contactMessageService.queryPage(pageNum, pageSize, contactMessage);
            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.error("分页查询联系消息失败: " + e.getMessage());
        }
    }

    /**
     * 创建联系消息
     */
    @Operation(summary = "创建联系消息", description = "新增一条联系消息信息")
    @PostMapping
    public Result<Long> create(
            @Parameter(description = "联系消息信息", required = true) 
            @RequestBody ContactMessage contactMessage) {
        try {
            int result = contactMessageService.insert(contactMessage);
            if (result > 0) {
                return Result.success("创建联系消息成功", contactMessage.getId());
            } else {
                return Result.error("创建联系消息失败");
            }
        } catch (Exception e) {
            return Result.error("创建联系消息失败: " + e.getMessage());
        }
    }

    /**
     * 更新联系消息
     */
    @Operation(summary = "更新联系消息", description = "更新指定ID的联系消息信息")
    @PutMapping("/{id}")
    public Result<Long> update(
            @Parameter(description = "联系消息ID", required = true) 
            @PathVariable Long id,
            @Parameter(description = "联系消息信息", required = true) 
            @RequestBody ContactMessage contactMessage) {
        try {
            contactMessage.setId(id);
            int result = contactMessageService.update(contactMessage);
            if (result > 0) {
                return Result.success("更新联系消息成功", id);
            } else {
                return Result.error("更新联系消息失败");
            }
        } catch (Exception e) {
            return Result.error("更新联系消息失败: " + e.getMessage());
        }
    }

    /**
     * 删除联系消息
     */
    @Operation(summary = "删除联系消息", description = "根据ID删除指定的联系消息信息")
    @DeleteMapping("/{id}")
    public Result<Long> delete(
            @Parameter(description = "联系消息ID", required = true) 
            @PathVariable Long id) {
        try {
            int result = contactMessageService.deleteById(id);
            if (result > 0) {
                return Result.success("删除联系消息成功", id);
            } else {
                return Result.error("删除联系消息失败");
            }
        } catch (Exception e) {
            return Result.error("删除联系消息失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除联系消息
     */
    @Operation(summary = "批量删除联系消息", description = "根据ID列表批量删除联系消息信息")
    @DeleteMapping("/batch")
    public Result<List<Long>> deleteBatch(
            @Parameter(description = "联系消息ID列表", required = true) 
            @RequestBody List<Long> ids) {
        try {
            List<Long> successIds = contactMessageService.deleteBatch(ids);
            return Result.success(
                String.format("成功删除 %d 条记录", successIds.size()), successIds);
        } catch (Exception e) {
            return Result.error("批量删除联系消息失败: " + e.getMessage());
        }
    }
}